package com.appspot.smartgan;

import org.json.JSONException;
import org.json.JSONObject;

import com.appspot.smartgan.server.Client;
import com.appspot.smartgan.server.HttpClient;
import com.appspot.smartgan.server.UrlGenerator;
import com.appspot.smartgan.util.Child;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ChildInfoFragment extends Fragment {

	public static final String FIRST_NAME = "first_name";
	
	public static final String LAST_NAME = "last_name";
	
	public static final String ADDRESS = "address";
	
	public static final String PHONE = "phone_number";
	
	public static final String DATE_OF_BIRTH = "date_of_birth";
	
	public static final String TEACHER_FIRST_NAME = "CT_first_name";
	
	public static final String TEACHER_LAST_NAME = "CT_last_name";
	
	public static final String SCHOOL_NAME = "KG_name";
	
	private Child child;
	
	private JSONObject childInfo;
	private JSONObject schoolInfo;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_child_info, container, false);
		
		
		// display child information
		displayChildInformation(rootView, childInfo);
		
		// display school information
		displaySchoolInformation(rootView, schoolInfo);
		
		// return the view
		return rootView;
	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// retrieve child info from server
		String url = UrlGenerator.getChildInfo(child.getId());
		Client client = new HttpClient(url, Client.RESPONSE_JSON);
		client.execute();

		// get child information
		childInfo = (JSONObject) client.getResponse();
		
		// retrieve school info from server
		url = UrlGenerator.getSchoolInfo(child.getSchoolId());
		client = new HttpClient(url, Client.RESPONSE_JSON);
		client.execute();
		
		// get school information
		schoolInfo = (JSONObject) client.getResponse();
	}

	public void setChild(Child child) {
		this.child = child;
	}

	
	// private methods
	
	/*
	 * no exception is thrown in the displayChildInformation and displaySchoolInformation
	 * because the server always returns a json object with those fields
	 */
	private void displayChildInformation(View rootView, JSONObject info) {
		try {
			TextView childInformation = 
					(TextView) rootView.findViewById(R.id.child_information_text_view);
			childInformation.setText(child.getName());
			
			// display address
			TextView addressTextView = 
					(TextView) rootView.findViewById(R.id.address_text_view);
			addressTextView.setText(addressTextView.getText() + 
					" " + info.getString(ADDRESS));
			
			// display phone number
			TextView phoneTextView = 
					(TextView) rootView.findViewById(R.id.phone_text_view);
			phoneTextView.setText(phoneTextView.getText() + 
					" " + info.getString(PHONE));
			
			// display birth date
			TextView birthDateTextView = 
					(TextView) rootView.findViewById(R.id.birthdate_text_view);
			birthDateTextView.setText(birthDateTextView.getText() + 
					" " + info.getString(DATE_OF_BIRTH));
		} catch (JSONException e) {
			// unreachable code
			Log.e("SMARTGAN", "json error display child info");
		}
	}
	
	private void displaySchoolInformation(View rootView, JSONObject info) {
		try {
			TextView schoolInfoTextView = 
					(TextView) rootView.findViewById(R.id.school_information_text_view);
			schoolInfoTextView.setText(info.getString(SCHOOL_NAME));
			
			TextView teacherNameTextView = 
					(TextView) rootView.findViewById(R.id.teacher_name_text_view);
			teacherNameTextView.setText(teacherNameTextView.getText() + 
					" " +info.getString(TEACHER_FIRST_NAME) + 
					" " + info.getString(TEACHER_LAST_NAME));
			
			TextView addressTextView = 
					(TextView) rootView.findViewById(R.id.school_address_text_view);
			addressTextView.setText(addressTextView.getText() + 
					" " + info.getString(ADDRESS));
			
			TextView phoneTextView = 
					(TextView) rootView.findViewById(R.id.school_phone_text_view);
			phoneTextView.setText(phoneTextView.getText() + 
					" " + info.getString(PHONE));
		} catch (JSONException e) {
			// unreachable code
			Log.e("SMARTGAN", "json error display school info\n" + e.getMessage());
		}
	}
	
}
